package com.example.ndkjnidemo.dashboard.bitmap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.LogUtils;
import com.example.ndkjnidemo.R;
import com.example.ndkjnidemo.databinding.ActivityBitmapBinding;

/**
 *
 */
public class BitmapActivity extends AppCompatActivity {

    private static final String TAG = "BitmapActivity";
    private ActivityBitmapBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityBitmapBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        //===
        onJniBitmap();
    }

    public void onJniBitmap() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.normal);
        int color = bitmap.getPixel(0, 0);
        LogUtils.i(TAG, "java getPixel[0][0] " + color + "=" + Integer.toHexString(color));
        binding.ivOrigin.setImageBitmap(bitmap);

        JNIBitmap jniBitmap = new JNIBitmap();

        // negative
        long startNegative = System.currentTimeMillis();
        Bitmap negativeBitmap = jniBitmap.negative(bitmap);
        if (negativeBitmap != null) {
            LogUtils.i(TAG, "negative cost:" + (System.currentTimeMillis() - startNegative));
            binding.ivNegative.setImageBitmap(negativeBitmap);
            // 对比原始图片的相同像素点的像素值
            int colorNegative = negativeBitmap.getPixel(0, 0);
            LogUtils.i(TAG, "java getPixel[0][0] " + colorNegative + "=" + Integer.toHexString(colorNegative));
        }
        // cold
        long startCold = System.currentTimeMillis();
        Bitmap coldBitmap = jniBitmap.cold(bitmap);
        if (coldBitmap != null) {
            LogUtils.i(TAG, "cold cost:" + (System.currentTimeMillis() - startCold));
            binding.ivCold.setImageBitmap(coldBitmap);
        }
        // warm
        long startWarm = System.currentTimeMillis();
        Bitmap wramBitmap = jniBitmap.warm(bitmap);
        if (wramBitmap != null) {
            LogUtils.i(TAG, "warm cost:" + (System.currentTimeMillis() - startWarm));
            binding.ivWarm.setImageBitmap(wramBitmap);
        }
        // blackWhite
        long startBlackWhite = System.currentTimeMillis();
        Bitmap blackWhiteBitmap = jniBitmap.blackAndWhite(bitmap);
        if (blackWhiteBitmap != null) {
            LogUtils.i(TAG, "blackWhite cost:" + (System.currentTimeMillis() - startBlackWhite));
            binding.ivBlackWhite.setImageBitmap(blackWhiteBitmap);
        }
        // gray
        long startGray = System.currentTimeMillis();
        Bitmap grayBitmap = jniBitmap.gray(bitmap);
        if (grayBitmap != null) {
            LogUtils.i(TAG, "gray cost:" + (System.currentTimeMillis() - startGray));
            binding.ivGray.setImageBitmap(grayBitmap);
        }
        // leftRight
        long startLeftRight = System.currentTimeMillis();
        Bitmap leftRightBitmap = jniBitmap.leftRight(bitmap);
        if (leftRightBitmap != null) {
            LogUtils.i(TAG, "leftRight cost:" + (System.currentTimeMillis() - startLeftRight));
            binding.ivLeftRight.setImageBitmap(leftRightBitmap);
        }
        // negativeChange
        long startNegativeChange = System.currentTimeMillis();
        if (jniBitmap.negativeChangeOrigin(bitmap) == 1) {
            LogUtils.i(TAG, "negativeChange cost:" + (System.currentTimeMillis() - startNegativeChange));
            binding.ivNegativeChange.setImageBitmap(bitmap);
        }
    }

}